<!-- 主诉添加，修改 -->
<template>
	<view class="container">
		<lk-topbar :title="'主诉'" :backcolor="'#f5f5f5'" @back="back"></lk-topbar>
		<view class="p30">
			<view class="recordItem">
				<view class="recordChild">
					<view class="recordName">主诉</view>
				</view>
				<view class="recordChild">
					<view class="">{{name?name:'点击选择'}}</view>
					<view class="icon arrow"></view>
				</view>
			</view>
			<lk-lists v-show="showSym" :list="symptoms" :actv="'#6aa1a8'" :isAdd="isAdd" :act="sym" :name='"symptoms"' @addData="addData"
				@select="select"></lk-lists>
			<view class="recordItem">
				<view class="recordChild">
					<view class="recordName">部位(可多选)</view>
				</view>
				<view class="recordChild">
					<view class="" @click="showDetail(0)">{{where?where:'点击选择'}}</view>
					<view class="icon arrow"></view>
				</view>
			</view>
			<view class="lists" v-if="showArr[0]">
				<view class="listBox">
					<view class="listItem" :style="{'background-color':posi.indexOf(index) != -1  ? '#6aa1a8':'#b4b4b4'}"
						@click="select({name:'position',content:item})" v-for="(item,index) in position" :key="index">
						{{item}}
					</view>
				</view>
				<view class="addOther" @click="addData('position')">添加其他</view>
			</view>
			<template v-if="pindex == -1">
				<view class="recordItem">
					<view class="recordChild">
						<view class="recordName">程度</view>
					</view>
					<view class="recordChild">
						<view class="" @click="showDetail(1)">{{sname?sname:'点击选择'}}</view>
						<view class="icon arrow"></view>
					</view>
				</view>
				<lk-lists v-if="showArr[1]" :list="degree" :isAdd="isAdd"  :actv="'#6aa1a8'"  :name='"degree"' :act="whei"
					@addData="addData" @select="select"></lk-lists>
			</template>
		</view>
		<button @click="save"  class="btn" type="default">保存</button>
		<lk-addipt v-show="showAdd" @addSubmit='addSubmit' @close="close"></lk-addipt>
	</view>
</template>

<script>
	import lkLists from "../../components/lk-lists/index.vue"
	import lkAddipt from "../../components/lk-addipt/index.vue"
	export default {
		name: "",
		data() {
			return {
				isAdd: false,
				showSym: true,
				showArr: [false, false],
				showAdd: false,
				symptoms: ['震颤', '僵直', '步态异常', '迟缓', '启动困难', '平衡障碍', '异动', '无力', '麻木'],
				position: ['全身', '面部', '左上肢', '颈部', '右上肢', '左下肢', '腰背部', '右下肢'],
				degree: ['无', '轻度', '中度', '重度', '无法忍受'],
				sid: '',
				sym: -1,
				name: "",
				posi: [],
				where: '',
				whei: -1,
				severity: '',
				sname: '',
				pindex: -1,
				cindex: 0,
				isSave: true,
			}
		},
		props: {

		},
		components: {
			lkLists,
			lkAddipt
		},
		onLoad(o) {
			this.type = o.type;
			if (typeof o.pi != 'undefined') {
				this.pindex = o.pi
			}
			this.cindex = o.ci;
			if (typeof o.data != "undefined") {
				o.data = JSON.parse(o.data);
				this.showSym = false;
				this.name = o.data.name;
				this.sym = this.symptoms.indexOf(this.name);
				this.where = o.data.where;
				o.data.where.split(',').forEach(item => {
					let i = this.position.indexOf(item);
					if (i != -1) {
						this.posi.push(i);
					}
				})
				if (this.pindex == -1) {
					this.sname = o.data.sname;
					this.whei = this.degree.indexOf(this.sname);
				}

				this.severity = o.data.severity
				
			}
			let pages = getCurrentPages();
			let pre = pages[pages.length -2];
			delete(pre.data.med);
		},
		onShow() {

		},
		methods: {
			// 保存症状数据
			save() {
				let pages = getCurrentPages();
				let pre = pages[pages.length - 2];
				if(this.name == ''){
					uni.showToast({
						title:"请选择主诉",
						icon:"error"
					})
					return false;
				}
				if(this.where == ''){
					uni.showToast({
						title:"请选择部位",
						icon:"error"
					})
					return false;
				}
				if(this.sname == '' && this.type != "diary"){
					uni.showToast({
						title:"请选择程度",
						icon:"error"
					})
					return false;
				}
				switch (this.name) {
					case '僵直':
						this.sid = "SM003"
						break;
					case '迟缓':
						this.sid = "SM017"

						break;
					case '震颤':
						this.sid = "SM018"

						break;
					case '步态异常':
						this.sid = "SM013"

						break;
					case '启动困难':
						this.sid = "SM014"

						break;
					case '平衡障碍':
						this.sid = "SM015"

						break;
					case '异动':
						this.sid = "SM022"

						break;
					case '无力':
						this.sid = "SM023"

						break;
					case '麻木':
						this.sid = "SM024"

						break;
					default:
						break;
				}
				if (this.pindex == -1) {
					switch (this.sname) {
						case '无':
							this.severity = 0;
							break;
						case '轻度':
							this.severity = 1;
							break;
						case '中度':
							this.severity = 2;
							break;
						case '重度':
							this.severity = 3;
							break;
						case '无法忍受':
							this.severity = 4;
							break;
						default:
							break;
					}
				}
				pre.setData({
					med: {
						id: this.sid,
						where: this.where,
						severity: this.severity,
						change:0
					},
					cindex: this.cindex,
					pindex: this.pindex,
				})
				this.isSave = true;
				this.$routeJump.back()
			},
			/**
			 * @param {Object} o 返回的值和哪一项
			 */
			select(o) {
				this.isSave = false;
				switch (o.name) {
					case 'symptoms':
						this.name = o.content;
						this.sym = this.symptoms.indexOf(this.name);
						break;
					case 'position':

						let whereArr = this.where.split(',');
						if (whereArr.indexOf(o.content) == -1) {
							if (this.where == '') {
								this.where += o.content;
							} else {
								this.where += ',' + o.content;
							}
							this.posi.push(this.position.indexOf(o.content))
						} else {
							let pi = whereArr.indexOf(o.content);
							this.posi.splice(pi, 1);
							whereArr.splice(pi, 1)
							this.where = whereArr.join(',');
						}
						break;
					case 'degree':
						this.sname = o.content;
						this.whei = this.degree.indexOf(this.sname);
						break;
					default:
						break;
				}
			},
			/** 确定添加
			 * @param {Object} data 添加的内容
			 */
			addSubmit(data) {
				this.isSave = false;
				switch (this.addName) {
					case 'symptoms':
						this.symptoms.push(data);
						break;
					case 'position':
						this.position.push(data);
						break;
					case 'degree':
						this.degree.push(data);
						break;
					default:
						break;
				}
				this.showAdd = false;
			},
			/** 显示添加弹窗
			 * @param {Object} name 添加哪项
			 */
			addData(name) {

				this.addName = name;
				this.showAdd = true;
			},
			/**
			 * 显示详情列表
			 * @param {Object} index 哪一个
			 */
			showDetail(index) {
				this.showArr.splice(index, 1, !this.showArr[index])
			},
			// 关闭添加弹窗
			close() {
				this.showAdd = false;
			},
			// 页面返回
			back() {
				let _this = this;
				if (_this.isSave) {
					_this.$routeJump.back()
				} else {
					wx.showModal({
						title: "提示",
						content: "你还没保存，是否保存？",
						success(res) {
							if (res.confirm) {
								_this.save()
							} else {
								_this.$routeJump.back()
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #f5f5f5;

		.recordItem {
			padding: 20rpx 0;
			font-size: 36rpx;
			@include flex(space-between, center);

			.recordChild {
				@include flex(flex-start, center) .recordName {
					width: 250rpx
				}
			}

		}

		.lists {
			width: 100%;
			height: auto;
			padding: 16rpx 0;
			background-color: #fff;
			border-radius: 16rpx;
			.listBox {

				.listItem {
					display: inline-block;
					padding: 10rpx 12rpx;
					margin: 10rpx 10rpx 0;
					border-radius: 10rpx;
					font-size: 32rpx;
					color: #fff;
					background-color: #b4b4b4;
				}
			}

			.actv {
				background-color: #ffb526 !important;
			}

			.addOther{
				display: inline-block;
				padding: 6rpx 12rpx;
				float: right;
				background-color: #6aa1a8;
				color: #fff;
				font-size: 32rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
				margin-top: 40rpx;
			}

			&:after {
				clear: both;
				content: "";
				display: block;
			}
		}
		.btn{
			width: 75%;
			height: 84rpx;
			line-height: 84rpx;
			margin: 100rpx auto 50rpx;
			border-radius: 50rpx;
			background-color: #6aa1a8;
		}
	}
</style>
